/*
var ref = require('ref');
console.log("require('ref')");

var ffi = require('ffi');
console.log("require('ffi')");

function TEXT(text){
return new Buffer(text, 'ucs2').toString('binary');
}
var user32 = new ffi.Library('user32', {
'MessageBoxW':
[
'int32', [ 'int32', 'string', 'string', 'int32' ]
]
});
var result = user32.MessageBoxW(0, TEXT('I am Node.JS!'), TEXT('Hello, World!'), 1);
console.log("result:: ", result); 
*/

var ref = require('ref'); //数据指针
var ffi = require('ffi'); //使用Javascript调用动态库
var refStruct = require('ref-struct'); //结构体
var refArray = require('ref-array'); // 用Buffer来实现C语言中的 array 数据类型

function TEXT(text){
    return new Buffer(text, 'ascii').toString('binary');
}

function UCS2TEXT(text){
    return new Buffer(text, 'ascii').toString('ucs2');
}

var ID_CODE = refArray('char', 6);

var ID_PASS = refArray('char', 6);

/**
 * 执法记录仪信息
 */
var ZFY_INFO = refStruct({
    'cSerial': refArray('char', 8),
    'userNo': refArray('char', 7),
    'userName': refArray('char', 33),
    'unitNo': refArray('char', 13),
    'unitName': refArray('char', 33)
});
/**
 * 执法记录仪密码
 */
var ZFY_PASSWD = refStruct({
	'userType': ref.types.uchar,
    'Manage_passwd': refArray('char', 6),
    'User_passwd': refArray('char', 6),
    'Default_passwd': refArray('char', 6)
});
/**
 * 执法记录仪参数
 */
var ZFY_PARAM = refStruct({
    'dev_param': refArray('char', 512)
});
/**
 * 对动态库里的函数进行声明
 */
var zfyInfoLib = new ffi.Library('../ZFYINFO', {
    'GetIDCode': ['int32', [ref.refType(ID_CODE), ref.refType('ushort')]],
    'Init_Device': ['int32', [ref.refType(ID_CODE), ref.refType('ushort')]],
    'GetZFYPassword': ['int32', [ref.refType(ZFY_PASSWD), ref.refType('ushort')]],
    'GetZFYInfo': ['int32', [ref.refType(ZFY_INFO), 'string', ref.refType('ushort')]],
    //'Get_Version_Info': ['int32', [refArray(ZFY_PARAM), ref.refType('ushort')]]
});

var bool, result, outNumber, idCode;

//实例化
idCode = ref.alloc(ID_CODE);
outNumber = ref.alloc('ushort');
bool = zfyInfoLib.GetIDCode(idCode, outNumber);

result = outNumber.deref();
console.log("bool:: ", bool);
console.log("result:: ", result);
console.log("id code:: ", idCode);

outNumber = ref.alloc('ushort');
bool = zfyInfoLib.Init_Device(idCode, outNumber);
result = outNumber.deref();
console.log("bool:: ", bool);
console.log("result:: ", result);

zfyPasswd = ref.alloc(ZFY_PASSWD);
outNumber = ref.alloc('ushort');
zfyPasswd.ref().userType = 1;
console.log("password:: ", zfyPasswd);
bool = zfyInfoLib.GetZFYPassword(zfyPasswd, outNumber);
result = outNumber.deref();
console.log("bool:: ", bool);
console.log("result:: ", result);
zfyPasswd = zfyPasswd.deref();
console.log("password:: ", zfyPasswd.Manage_passwd);

zfyInfo = ref.alloc(ZFY_INFO);
outNumber = ref.alloc('ushort');
bool = zfyInfoLib.GetZFYInfo(zfyInfo, TEXT(zfyPasswd.Manage_passwd), outNumber);
result = outNumber.deref();
console.log("bool:: ", bool);
console.log("result:: ", result);
zfyInfo = zfyInfo.deref();
var serial = TEXT(zfyInfo.cSerial);
var userName = UCS2TEXT(zfyInfo.userName);
console.log("zfy serial:: ", serial);
console.log("zfy user name:: ", userName);



//var zfyParam = new ZFY_PARAM();
//outNumber = ref.alloc('ushort');
//bool = zfyInfoLib.Get_Version_Info(zfyParam.ref(), outNumber);
//result = outNumber.unref();
//console.log("result:: ", result);